# -*- coding: utf-8 -*-
"""
Created on Sun Mar  3 21:12:50 2019

@author: william

Email: hua_yan_tsn@163.com
"""
import numpy as np
def getPositionEmbeddings(sentence, target_word, word_ids = None):
    """
    :param sentence: the sentence need to be position embedded, special, there is no puncatuation mark in sentence
    :param target_word: the target aspect word
    :return : the dict of word with position embeddings as its value
    """
    sentence_list = sentence.split()
    target_word_list = target_word.split()
    index = findIndex(sentence_list, target_word_list)
    if (index == -1):
        print('not found\naspect word', target_word_list)
        print('sentent', sentence_list)
        return []
    dicts = []
    for i in range(len(sentence_list)):
        if i < index:
            dicts.append((sentence_list[i], i - index))
        elif(i >= index and i < index + len(target_word_list)):
            dicts.append((sentence_list[i], 0))
        elif(i >= index + len(target_word_list)):
            dicts.append((sentence_list[i], i - index - len(target_word_list) + 1))
    position_input = []
    for i in dicts:
        position_input.append(word_ids[i[1]])
    return position_input

def findIndex(sentence_list, target_word_list):
    """
    :param sentence_list:
    :param target_word_list:
    :return index: the index of the first word in target_word
    """
    index = -1
    for i in range(len(sentence_list)):
        if (sentence_list[i] == target_word_list[0]):
            index = i
            for j in range(1, len(target_word_list)):
                if (sentence_list[i + j] != target_word_list[j]):
                    index = -1
                    break
            if (index != -1):
                break
    return index

